<?php
class user{
    var $table;
    protected $logged = 0;
    var $email = '';
    var $uid = -1;
    var $facebook_id = 0;
    var $avatar = '';
    var $linkfb = '';
    var $name = '';
    var $firstname = '';
    var $lastname = '';
    var $gender = '';
    var $password = '';
    var $login_fb = 0;
    var $isAdmin = 0;

    function __construct($email = '', $pass = '', $login_fb = 0){
        $this->table = 'users';
        $this->logged = 0;
        if($email == ''){
            //lấy email từ cache
            if(isset($_COOKIE[md5('login_email')])){
                $email = base64_decode($_COOKIE[md5('login_email')]);
            }
        }
        if($pass == ''){
            if(isset($_COOKIE[md5('login_pass')])){
                $pass = base64_decode($_COOKIE[md5('login_pass')]);
            }
            else if(isset($_COOKIE[md5('login_fb')]) && $_COOKIE[md5('login_fb')] == 1){
                $this->login_fb = 1;
            };
        }
        if($login_fb == 1){
            $this->login_fb = 1;
        }
        if($email == '' && $pass == '') return;
        else{
            if($this->login_fb == 1){
                //check dang nhap qua facebook roi
                $sql = 'SELECT * FROM '.$this->table.' WHERE use_email = "'.$email.'"';
            }else{
                //Luu 1 cap nua - cap security
                $db_security = new db_query('SELECT use_security FROM '.$this->table . ' WHERE use_email = "'.$email.'"');
                $security = mysql_fetch_assoc($db_security->result);unset($db_security);
                //Tao chuoi hash password de check
                $pass_check = md5($pass . $security['use_security']);
                $sql = 'SELECT use_id,use_firstname,use_facebook_id,
                                use_lastname,use_email,use_name,use_gender,
                                use_linkfb,use_avatar,use_avatarfb,use_isAdmin  
                        FROM '.$this->table. ' 
                        WHERE use_email = "'.$email.'" 
                            AND use_password = "'.$pass_check.'"';
            }
        }
        $db_query = new db_query($sql);
        if($row = mysql_fetch_assoc($db_query->result)){
            $this->logged = 1;
            $this->uid = $row['use_id'];
            $this->facebook_id = $row['use_facebook_id'];
            $this->email = $row['use_email'];
            $this->name = $row['use_name'];
            $this->firstname = $row['use_firstname'];
            $this->lastname = $row['use_lastname'];
            $this->avatar = $row['use_avatar'] ? $row['use_avatar'] : '/themes_v2/img/no_avatar.jpg';
            $this->avatarfb = $row['use_avatarfb'];
            $this->gender = $row['use_gender'];
            $this->linkfb = $row['use_linkfb'];
            $this->password = $pass;
            $this->isAdmin = $row['use_isAdmin'];
        }
        unset($db_query);
    }
    function savecookie($time = 0){
        if ($this->logged!=1) return false;
        if($_SERVER['SERVER_NAME'] == 'localhost'){
            if($time > 0){
                setcookie(md5('login_email'),base64_encode($this->email),time()+$time,"/");
                if($this->login_fb) {
                    setcookie(md5('login_fb'),1,time()+$time,"/");   
                }
                else {
                    setcookie(md5('login_pass'),base64_encode($this->password),time()+$time,"/");
                }   
            }else{
                setcookie(md5('login_email'),base64_encode($this->email),null,"/");
                if($this->login_fb) {
                    setcookie(md5('login_fb'),1,null,"/");   
                }
                else {
                    setcookie(md5('login_pass'),base64_encode($this->password),null,"/");
                } 
            }
            
        }else{
            if ($time > 0){
                setcookie(md5('login_email'),base64_encode($this->email),time()+$time,"/", 'vcamp.vn');
                setcookie(md5('login_email'),base64_encode($this->email),time()+$time,"/", '.vcamp.vn');
                if($this->login_fb) {
                    setcookie(md5('login_fb'),1,time()+$time,"/", 'vcamp.vn');
                    setcookie(md5('login_fb'),1,time()+$time,"/", '.vcamp.vn');    
                }
                else {
                    setcookie(md5('login_pass'),base64_encode($this->password),time()+$time,"/", 'vcamp.vn');
                    setcookie(md5('login_pass'),base64_encode($this->password),time()+$time,"/", '.vcamp.vn');
                }
            }
            else{
                setcookie(md5('login_email'),base64_encode($this->email),null,"/", 'vcamp.vn');
                setcookie(md5('login_email'),base64_encode($this->email),null,"/", '.vcamp.vn');
                if($this->login_fb) {
                    setcookie(md5('login_fb'),1,null,"/", 'vcamp.vn');
                    setcookie(md5('login_fb'),1,null,"/", '.vcamp.vn');
                }
                else {
                    setcookie(md5('login_pass'),base64_encode($this->password),null,"/", 'vcamp.vn');
                    setcookie(md5('login_pass'),base64_encode($this->password),null,"/", '.vcamp.vn');
                }
            }    
        }
        
    }
    function logout(){
        ob_start();
        setcookie(md5('login_pass'),null,null,"/", 'vcamp.vn');
        setcookie(md5('login_pass'),null,null,"/", '.vcamp.vn');

        setcookie(md5('login_fb'),null,null,'/','vcamp.vn');
        setcookie(md5('login_fb'),null,null,'/','.vcamp.vn');

        setcookie(md5('login_email'),null,null,"/", 'vcamp.vn');
        setcookie(md5('login_email'),null,null,"/", '.vcamp.vn');

        unset($_COOKIE[md5('login_email')]);
        unset($_COOKIE[md5('login_pass')]);
        unset($_COOKIE[md5('login_fb')]);

        $this->logged=0;
    }
    function logged(){
        return $this->logged;
    }
}
?>